<?php

namespace app\admin\controller\system;

use albert\services\JsonService;
use albert\services\UtilService;
use app\admin\controller\AuthController;
use app\model\system\SystemGroup;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 系统分组控制器
 * Class GroupController
 * @package app\admin\controller\system
 */
class GroupController extends AuthController
{
    /**
     * 数据组展示页
     * Author: Albert3306
     * Date: 2021/7/5
     * @throws DbException
     */
    public function index(): string
    {
        return $this->fetch('system/group/index', SystemGroup::page());
    }

    /**
     * 添加数据组
     * Author: Albert3306
     * Date: 2021/7/7
     * @return string
     */
    public function create(): string
    {
        return $this->fetch('system/group/create');
    }

    /**
     * 添加/编辑保存
     * Author: Albert3306
     * Date: 2021/7/7
     * @param int $id
     * @return string
     */
    public function save(int $id = 0): string
    {
        $data = UtilService::param([
            ['name', ''],
            ['info', ''],
            ['type_list', ''],
            ['config_name', '']
        ], $this->request);

        // 数据组名称判断
        if(!$data['name']) return JsonService::fail('请输入数据组名称！');
        if(!$data['config_name']) return JsonService::fail('请输入配置名称！');

        // 判断ID是否存在，存在就是编辑，不存在就是添加
        if(!$id){
            if(SystemGroup::be($data['config_name'], 'config_name')) return JsonService::fail('数据关键字已存在！');
        }

        // 字段信息判断
        if(!count($data['type_list']))
            return JsonService::fail('字段至少存在一个！');
        else {
            $validate = ['name', 'type', 'title', 'description'];
            foreach ($data['type_list'] as $key => $val) {
                foreach ($val as $name => $field) {
                    if(empty($field['value']) && in_array($name, $validate))
                        return JsonService::fail('字段' . ($key + 1) . '：' . $field['placeholder'] . '不能为空！');
                    else
                        $data['fields'][$key][$name] = $field['value'];
                }
            }
        }
        $data['fields'] = htmlspecialchars_decode(json_encode($data['fields']));
        // 断ID是否存在，存在就是编辑，不存在就是添加
        if (!$id) {
            SystemGroup::create($data);
            return JsonService::successful('添加数据组成功！');
        } else {
            SystemGroup::edit($data, $id);
            return JsonService::successful('编辑数据组成功！');
        }
    }

    /**
     * 编辑数据组
     * Author: Albert3306
     * Date: 2021/7/7
     * @param $id
     * @return string
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function edit($id): string
    {
        $info = SystemGroup::get($id);
        if (!$info) return $this->failed('数据不存在！');
        $fields = json_decode($info['fields'], true);
        $typeList = [];
        foreach ($fields as $key => $v){
            $typeList[$key]['name']['value'] = $v['name'];
            $typeList[$key]['title']['value'] = $v['title'];
            $typeList[$key]['type']['value'] = $v['type'];
            $typeList[$key]['param']['value'] = $v['param'];
        }
        $info['fields'] = json_encode($typeList);
        return $this->fetch('system/group/edit', compact('info'));
    }
}